package com.zry.traced.presenter

import android.content.Intent
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView
import com.zry.traced.R
import com.zry.traced.adapter.FlowRecordAdapter
import com.zry.traced.adapter.OperationRecordAdapter01
import com.zry.traced.adapter.OutDetailAdapter
import com.zry.traced.adapter.StoreDetailListAdapter
import com.zry.traced.been.*
import com.zry.traced.retrofit.ShowUserView
import com.zry.traced.ui.activity.OperationRecordActivity
import com.zry.traced.ui.activity.OperationRecordInfoActivity
import com.zry.traced.utils.ACacheUtil
import kotlinx.android.synthetic.main.activity_operation_record.*

class OperationRecordPresenter(private val activity: OperationRecordActivity) : ShowUserView {

    private var pageStoreIndex = 1

    private var operationRecordBeanList: MutableList<FlowCode>? = null
    private var storeDetailList: MutableList<StoreDetail>? = null
    private var outDetailBeanList: MutableList<OutDetailBean>? = null
    private var flowRecordBeanList: MutableList<FlowRecord>? = null


    private var operationRecordAdapter01: OperationRecordAdapter01? = null
    private var storeDetailListAdapter: StoreDetailListAdapter? = null
    private var outDetailAdapter: OutDetailAdapter? = null
    private var flowRecordAdapter: FlowRecordAdapter? = null


    /**
     * 获取操作记录
     */

    fun getOperationRecord() {

        when (activity.title) {
            "入库记录" -> {
                storeDetailList = mutableListOf()
                initStoreAdapter()

                HttpServer(this).getStoreDetailList(ACacheUtil().getLoginInfo().userid, pageStoreIndex, 10, activity)
            }
            "出库记录" -> {
                outDetailBeanList = mutableListOf()
                initOutDetailAdapter()
                HttpServer(this).getOutDetailList(ACacheUtil().getLoginInfo().userid, pageStoreIndex, 10, activity)
            }
            "打包记录" -> {
                operationRecordBeanList = mutableListOf()
                initOperationAdapter()
                HttpServer(this).getFlowCodeList(ACacheUtil().getLoginInfo().EntID, ACacheUtil().getLoginInfo().userid, pageStoreIndex, 10, activity)
            }
            "补码记录" -> {
                flowRecordBeanList = mutableListOf()
                initFlowRecordAdapter()
                HttpServer(this).getFlowRecordList(ACacheUtil().getLoginInfo().userid, ACacheUtil().getLoginInfo().EntID, "2", pageStoreIndex, 10, activity)
            }
            "拆码记录" -> {
                flowRecordBeanList = mutableListOf()
                initFlowRecordAdapter()
                HttpServer(this).getFlowRecordList(ACacheUtil().getLoginInfo().userid, ACacheUtil().getLoginInfo().EntID, "1", pageStoreIndex, 10, activity)
            }
        }
    }


    val mLoadMoreListener: SwipeMenuRecyclerView.LoadMoreListener = SwipeMenuRecyclerView.LoadMoreListener {
        activity.operationRecordRecycler.postDelayed({
            when (activity.title) {
                "入库记录" -> {
                    HttpServer(this).getStoreDetailList(ACacheUtil().getLoginInfo().userid, pageStoreIndex, 10, activity)
                }
                "出库记录" -> {
                    HttpServer(this).getOutDetailList(ACacheUtil().getLoginInfo().userid, pageStoreIndex, 10, activity)
                }
                "打包记录" -> {
                    HttpServer(this).getFlowCodeList(ACacheUtil().getLoginInfo().EntID, ACacheUtil().getLoginInfo().userid, pageStoreIndex, 10, activity)
                }
                "补码记录" -> {
                    HttpServer(this).getFlowRecordList(ACacheUtil().getLoginInfo().userid, ACacheUtil().getLoginInfo().EntID, "2", pageStoreIndex, 10, activity)
                }
                "拆码记录" -> {
                    HttpServer(this).getFlowRecordList(ACacheUtil().getLoginInfo().userid, ACacheUtil().getLoginInfo().EntID, "1", pageStoreIndex, 10, activity)
                }
            }
        }, 1000)
    }


    override fun toMainActivity(what: Int, data: Any) {
        when (what) {
            //入库记录
            14 -> {
                val storeDetailBeanList: MutableList<StoreDetail> = data as MutableList<StoreDetail>
                if (pageStoreIndex == 1) {
                    if (storeDetailBeanList.size > 0) {
                        activity.operationRecordRecycler.loadMoreFinish(false, true)
                        storeDetailList!!.addAll(storeDetailBeanList)
                        storeDetailListAdapter?.notifyDataSetChanged()
                        pageStoreIndex++
                    }
                } else {
                    when {
                        storeDetailBeanList.size == 0 -> activity.operationRecordRecycler.loadMoreFinish(true, false)
                        storeDetailBeanList.size > 0 -> {
                            pageStoreIndex++
                            storeDetailList!!.addAll(storeDetailBeanList)
                            storeDetailListAdapter?.notifyDataSetChanged()
                            activity.operationRecordRecycler.loadMoreFinish(false, true)
                        }
                        else -> activity.operationRecordRecycler.loadMoreError(0, "请求网络失败")
                    }
                }
            }
            8 -> {
                val operationRecordList = data as MutableList<FlowCode>
                if (pageStoreIndex == 1) {
                    if (operationRecordList.size > 0) {
                        activity.operationRecordRecycler.loadMoreFinish(false, true)
                        operationRecordBeanList!!.addAll(operationRecordList)
                        operationRecordAdapter01?.notifyDataSetChanged()
                        pageStoreIndex++
                    }
                } else {
                    when {
                        operationRecordList.size == 0 -> activity.operationRecordRecycler.loadMoreFinish(true, false)
                        operationRecordList.size > 0 -> {
                            pageStoreIndex++
                            operationRecordBeanList!!.addAll(operationRecordList)
                            operationRecordAdapter01?.notifyDataSetChanged()
                            activity.operationRecordRecycler.loadMoreFinish(false, true)
                        }
                        else -> activity.operationRecordRecycler.loadMoreError(0, "请求网络失败")
                    }
                }
            }

            //出库记录
            19 -> {
                val outDetailList = data as MutableList<OutDetailBean>
                if (pageStoreIndex == 1) {
                    if (outDetailList.size > 0) {
                        activity.operationRecordRecycler.loadMoreFinish(false, true)
                        outDetailBeanList!!.addAll(outDetailList)
                        outDetailAdapter?.notifyDataSetChanged()
                        pageStoreIndex++
                    }
                } else {
                    when {
                        outDetailList.size == 0 -> activity.operationRecordRecycler.loadMoreFinish(true, false)
                        outDetailList.size > 0 -> {
                            pageStoreIndex++
                            outDetailBeanList!!.addAll(outDetailList)
                            outDetailAdapter?.notifyDataSetChanged()
                            activity.operationRecordRecycler.loadMoreFinish(false, true)
                        }
                        else -> activity.operationRecordRecycler.loadMoreError(0, "请求网络失败")
                    }
                }
            }
            24 -> {
                val flowRecordList = data as MutableList<FlowRecord>
                if (pageStoreIndex == 1) {
                    if (flowRecordList.size > 0) {
                        activity.operationRecordRecycler.loadMoreFinish(false, true)
                        flowRecordBeanList!!.addAll(flowRecordList)
                        flowRecordAdapter?.notifyDataSetChanged()
                        pageStoreIndex++
                    }
                } else {
                    when {
                        flowRecordList.size == 0 -> activity.operationRecordRecycler.loadMoreFinish(true, false)
                        flowRecordList.size > 0 -> {
                            pageStoreIndex++
                            flowRecordBeanList!!.addAll(flowRecordList)
                            flowRecordAdapter?.notifyDataSetChanged()
                            activity.operationRecordRecycler.loadMoreFinish(false, true)
                        }
                        else -> activity.operationRecordRecycler.loadMoreError(0, "请求网络失败")
                    }
                }
            }
        }
    }

    private fun initStoreAdapter() {
        storeDetailListAdapter = StoreDetailListAdapter(R.layout.item_operation_record, storeDetailList)
        activity.recyclerViewUtilKt?.setAdapter(storeDetailListAdapter!!)
        storeDetailListAdapter?.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(activity, OperationRecordInfoActivity::class.java)
            intent.putExtra("title", activity.title)
            intent.putExtra("DetailID", storeDetailList!![position].DetailID)
            activity.startActivity(intent)
        }
    }

    private fun initOutDetailAdapter() {
        outDetailAdapter = OutDetailAdapter(R.layout.item_operation_record, outDetailBeanList)
        activity.recyclerViewUtilKt?.setAdapter(outDetailAdapter!!)
        outDetailAdapter?.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(activity, OperationRecordInfoActivity::class.java)
            intent.putExtra("title", activity.title)
            intent.putExtra("DetailID", outDetailBeanList!![position].DetailID)
            activity.startActivity(intent)
        }
    }

    private fun initOperationAdapter() {
        operationRecordAdapter01 = OperationRecordAdapter01(R.layout.item_operation_record, operationRecordBeanList)
        activity.recyclerViewUtilKt?.setAdapter(operationRecordAdapter01!!)
        operationRecordAdapter01?.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(activity, OperationRecordInfoActivity::class.java)
            intent.putExtra("title", activity.title)
            intent.putExtra("FlowCodeID", operationRecordBeanList!![position].FlowCodeID)
            activity.startActivity(intent)
        }
    }

    private fun initFlowRecordAdapter() {
        flowRecordAdapter = FlowRecordAdapter(R.layout.item_operation_record, flowRecordBeanList)
        activity.recyclerViewUtilKt?.setAdapter(flowRecordAdapter!!)
        flowRecordAdapter?.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(activity, OperationRecordInfoActivity::class.java)
            intent.putExtra("title", activity.title)
            intent.putExtra("RecordID", flowRecordBeanList!![position].RecordID)
            activity.startActivity(intent)
        }
    }

}